package net.neptech.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.PostConstruct;
import net.neptech.mapper.WorkflowGroupMapper;
import net.neptech.model.workflow.WorkflowGroup;
import net.neptech.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

/**
 * @Author Saya
 * @Date 2025/8/22 11:37
 * @Description TODO
 **/
@Service
public class GroupServiceImpl extends ServiceImpl<WorkflowGroupMapper, WorkflowGroup> implements GroupService {

    @Autowired
    private WorkflowGroupMapper workflowGroupMapper;
    @PostConstruct
    public void init(){}
    /**
     * 组列表
     */
    @Override
    public List<WorkflowGroup> queryList() {
        return this.lambdaQuery()
                .orderByDesc(WorkflowGroup::getSort).list();
    }
    /**
     * 新增表单分组
     */
    @Override
    public void create(WorkflowGroup workflowGroup) {
        List<WorkflowGroup> list = this.lambdaQuery().orderByDesc(WorkflowGroup::getSort).list();
        WorkflowGroup pg = new WorkflowGroup();
        pg.setSort(list.isEmpty()?0:list.get(0).getSort()+1);
        pg.setGroupName(workflowGroup.getGroupName());
        //生成随机
        Random random = new Random();
        Long rdm = (Long)System.currentTimeMillis();
        Integer rdm2 = (Integer) random.nextInt(9000)+1000;
        pg.setId(rdm.toString()+rdm2.toString());
        this.save(pg);
    }
    /**
     * 上移
     */
    @Override
    public void topSort(WorkflowGroup workflowGroup) {
        WorkflowGroup pg = this.getById(workflowGroup.getId());
        Integer sort = pg.getSort();
        List<WorkflowGroup> list = this.lambdaQuery()
                .gt(WorkflowGroup::getSort, sort)
                .orderByAsc(WorkflowGroup::getSort).list();
        WorkflowGroup pg1 = list.get(0);
        Integer sort1 = pg1.getSort();
        pg.setSort(sort1);
        this.updateById(pg);
        pg1.setSort(sort);
        this.updateById(pg1);
    }
    /**
     * 下移
     */
    @Override
    public void bottomSort(WorkflowGroup workflowGroup) {
        WorkflowGroup pg = this.getById(workflowGroup.getId());
        Integer sort = pg.getSort();
        List<WorkflowGroup> list = this.lambdaQuery()
                .lt(WorkflowGroup::getSort, sort)
                .orderByDesc(WorkflowGroup::getSort).list();
        WorkflowGroup pg1 = list.get(0);
        Integer sort1 = pg1.getSort();
        pg.setSort(sort1);
        this.updateById(pg);
        pg1.setSort(sort);
        this.updateById(pg1);
    }
    /**
     * 修改组
     */
    @Override
    public void edit(WorkflowGroup workflowGroup) {
        System.out.println(workflowGroup);
        WorkflowGroup pg = this.getById(workflowGroup.getId());
        pg.setGroupName(workflowGroup.getGroupName());
        BeanUtil.copyProperties(workflowGroup,pg, CopyOptions.create().setIgnoreNullValue(true));
        this.updateById(pg);
    }
    /**
     * 删除分组
     */
    @Override
    public void delete(long id) {
        workflowGroupMapper.deleteById(id);
    }
}
